/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.koller.million.grid.frame.plyManag;

import de.koller.million.grid.commands.AddGNode;
import de.koller.million.grid.engine.player.Player;
import de.koller.million.grid.frame.ply.PlayerFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author User
 */
public class PlayerManagerFrameComp extends javax.swing.JFrame {

	private final PlayerManagerFrame node;

	/**
	 * Creates new form PlayerManagerFrameComp
	 */
	public PlayerManagerFrameComp() {
		this.node = null;
		initComponents();
	}

	public PlayerManagerFrameComp( PlayerManagerFrame node ) {
		this.node = node;
		initComponents();
		
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        playerList = new javax.swing.JList();
        detailPanel = new javax.swing.JPanel();
        detailPanel.setVisible( false );
        detName = new javax.swing.JLabel();
        grp = new javax.swing.JToggleButton();
        jButton2 = new javax.swing.JButton();
        playerInGrp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Spieler verwalten");
        setLocationByPlatform(true);
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jButton1.setText("neuer Spieler");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        playerList.setModel(node.newPlayerList());
        playerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        playerList.setCellRenderer(new PMPlayerPanel());
        playerList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                playerListMouseClicked(evt);
            }
        });
        playerList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                playerListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(playerList);

        detailPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));

        detName.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        detName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        detName.setText("Name");

        grp.setText("in Gruppe");
        grp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                grpActionPerformed(evt);
            }
        });

        jButton2.setText("zeigen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout detailPanelLayout = new javax.swing.GroupLayout(detailPanel);
        detailPanel.setLayout(detailPanelLayout);
        detailPanelLayout.setHorizontalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(detName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(grp, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, detailPanelLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton2))
        );
        detailPanelLayout.setVerticalGroup(
            detailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailPanelLayout.createSequentialGroup()
                .addComponent(detName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(grp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2))
        );

        playerInGrp.setText("Spieler in Gruppe:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(detailPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(playerInGrp))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addComponent(detailPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(playerInGrp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void formWindowClosing( java.awt.event.WindowEvent evt ) {//GEN-FIRST:event_formWindowClosing
		node.closeThis();
	}//GEN-LAST:event_formWindowClosing

	private void jButton1ActionPerformed( java.awt.event.ActionEvent evt ) {//GEN-FIRST:event_jButton1ActionPerformed
		String i = JOptionPane.showInputDialog( null, "Bitte Spielernamen eingeben.", 
				"Neuer Spieler", JOptionPane.PLAIN_MESSAGE );
		if( i != null )
			node.createPlayer( i );
	}//GEN-LAST:event_jButton1ActionPerformed

	private void playerListMouseClicked( java.awt.event.MouseEvent evt ) {//GEN-FIRST:event_playerListMouseClicked
		
	}//GEN-LAST:event_playerListMouseClicked

	private void playerListValueChanged( javax.swing.event.ListSelectionEvent evt ) {//GEN-FIRST:event_playerListValueChanged
		if( playerList.getSelectedIndex() == -1 )
			return;
		
		Player p = (Player)playerList.getSelectedValue();
		detailPanel.setVisible( true );
		detName.setText( p.name );
		grp.setSelected( p.getGrp().contains( (int)'G' ) );
		this.pack();
	}//GEN-LAST:event_playerListValueChanged

	private void grpActionPerformed( java.awt.event.ActionEvent evt ) {//GEN-FIRST:event_grpActionPerformed
		Player p = (Player)playerList.getSelectedValue();
		if( p == null )
			return;
		
		if( grp.isSelected() ) {
			p.setGrp( (int)'G', true );
		} else {
			p.setGrp( (int)'G', false );
		}
	}//GEN-LAST:event_grpActionPerformed

	private void jButton2ActionPerformed( java.awt.event.ActionEvent evt ) {//GEN-FIRST:event_jButton2ActionPerformed
		Player p = (Player)playerList.getSelectedValue();
		if( p == null )
			return;
		
		node.groot.call( new AddGNode( node.groot.frame, new PlayerFrame( p ) ) );
	}//GEN-LAST:event_jButton2ActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main( String args[] ) {
		/*
		 * Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() ) {
				if( "Nimbus".equals( info.getName() ) ) {
					javax.swing.UIManager.setLookAndFeel( info.getClassName() );
					break;
				}
			}
		} catch ( ClassNotFoundException ex ) {
			java.util.logging.Logger.getLogger( PlayerManagerFrameComp.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( InstantiationException ex ) {
			java.util.logging.Logger.getLogger( PlayerManagerFrameComp.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( IllegalAccessException ex ) {
			java.util.logging.Logger.getLogger( PlayerManagerFrameComp.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		} catch ( javax.swing.UnsupportedLookAndFeelException ex ) {
			java.util.logging.Logger.getLogger( PlayerManagerFrameComp.class.getName() ).log( java.util.logging.Level.SEVERE, null, ex );
		}
		//</editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater( new Runnable() {

			public void run() {
				new PlayerManagerFrameComp().setVisible( true );
			}
		} );
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel detName;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JToggleButton grp;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JLabel playerInGrp;
    public javax.swing.JList playerList;
    // End of variables declaration//GEN-END:variables
}
